package com.code.petapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.code.petapp.R;
import com.code.petapp.bean.AdoptBean;

import java.util.List;

public class AdoptImgAdapter extends RecyclerView.Adapter<AdoptImgAdapter.MyViewHolder> {
    private Context context;
    private List<AdoptBean> list;
    private View inflater;
    private OnItemClickListener mListener;


    public AdoptImgAdapter(Context context, List<AdoptBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.adopt_img_item, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(inflater);
        return myViewHolder;
    }
    public interface OnItemClickListener {
        void onClick(View parent, int position);
    }
    // 设置点击事件
    public void setOnItemClickListener(OnItemClickListener l) {
        this.mListener = l;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        AdoptBean adoptBean=list.get(position);
        holder.name.setText(adoptBean.getAnimalName());
        Glide.with(holder.itemView.getContext())
                .load(adoptBean.getAnimalImg())
                .into(holder.imgAvatar);
        holder.time.setText(adoptBean.getTime());
        holder.status.setText(adoptBean.getStatus());
        if(null != mListener) {
            holder.btnBack.setOnClickListener(v -> mListener.onClick(holder.btnBack, position));
        }
        if(adoptBean.getStatus().equals("已归还")){
            holder.btnBack.setEnabled(false);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        ImageView imgAvatar;;
        TextView time;
        TextView status;
        Button btnBack;

        public MyViewHolder(View itemView) {//这个view参数就是recyclerview子项的最外层布局
            super(itemView);
            //可以通过findViewById方法获取布局中的TextView
            name =  itemView.findViewById(R.id.name);
            imgAvatar =  itemView.findViewById(R.id.imgAvatar);
            time =  itemView.findViewById(R.id.time);
            status =  itemView.findViewById(R.id.status);
            btnBack =  itemView.findViewById(R.id.btnBack);
        }
    }
}
